package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("cat")
public class TbItemCatController {

    //打印日志
    private static List<TbItemCat> list = new ArrayList(2);
    private Logger logger = LoggerFactory.getLogger(TbBrandController.class);

    @DubboReference
    private TbItemCatService catService;

    @DubboReference
    private TbTypeTemplateService templateService;

    @RequestMapping("show")
    public String show(){
        return "admin/item_cat";
    }

    @RequestMapping("listCat")
    @ResponseBody
    public Result listCat(@RequestParam(defaultValue = "0") Long pid, HttpSession session){
        try {
            session.setAttribute("pid",pid);
            List<TbItemCat> listCat = catService.listCat(pid);
            return Result.OK().setData(listCat);
        }catch (Exception e){
            logger.info("查询分类出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("listTemplate")
    @ResponseBody
    public Result listTemplate(){
        try {
            List<TbTypeTemplate> listT = templateService.selectTemplates();
            return Result.OK().setData(listT);
        }catch (Exception e){
            logger.info("查询模板出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("addCat")
    @ResponseBody
    public Result addCat(TbItemCat cat,HttpSession session){
        try {
            Long id = (Long) session.getAttribute("pid");
            cat.setParentId(id);
            catService.addCat(cat);
            return Result.OK().setData(id);
        }catch (Exception e){
            logger.info("新增分类出现异常: {}",e);
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            TbItemCat cat = catService.selectCatById(id);
            return Result.OK().setData(cat);
        }catch (Exception e){
            logger.info("新增分类出现异常: {}",e);
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }
    @RequestMapping("updateCat")
    @ResponseBody
    public Result updateCat(TbItemCat cat,HttpSession session){
        try {
            Long id = (Long) session.getAttribute("pid");
            catService.updateById(cat);
            return Result.OK().setData(id);
        }catch (Exception e){
            logger.info("新增分类出现异常: {}",e);
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("deleteCats")
    @ResponseBody
    public Result deleteCats(Long[] ids){
        try {
            Integer i = catService.deleteCatss(ids);
            return Result.OK().setData(i);
        }catch (Exception e){
            logger.info("删除分类出现异常: {}",e);
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("navaspector")
    @ResponseBody
    public Result navaspector(HttpSession session){
        try {
            Long id = (Long)session.getAttribute("pid");
            if(id != 0){
                TbItemCat cat = catService.selectCatById(id);
                if(list.size() > 0){
                    list.add(1,cat);
                }else {
                    list.add(0,cat);
                }
            }else{
                list.clear();
            }
            return Result.OK().setData(list);
        }catch (Exception e){
            logger.info("删除分类出现异常: {}",e);
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }

}

